#!/usr/bin/env python
# coding=utf-8

import os  
import sys  
from smtplib import SMTP  
from email.MIMEMultipart import MIMEMultipart  
from email.mime.application import MIMEApplication  
import time  
  
GMAIL_ACCOUNT = r'user@gmail.com'
GMAIL_PASSWD = r'passwd'

def send_file_to_gmail(filename):
	"""GMail file sender: Send a file use GMail. """
	config =  {  
	    'from': GMAIL_ACCOUNT,
	    'to': GMAIL_ACCOUNT,
	    'subject': '[gsend]Send file ' + filename,
	    'file': filename,
	    'server': 'smtp.gmail.com',  
	    'port': 587,  
	    'username': GMAIL_ACCOUNT,
	    'password': GMAIL_PASSWD,
	}  
	  
	print 'Preparing...',  
	  
	message = MIMEMultipart( )  
	message['from'] = config['from']  
	message['to'] = config['to']  
	message['Reply-To'] = config['from']  
	message['Subject'] = config['subject']  
	message['Date'] = time.ctime(time.time())  
	
	message['X-Priority'] =  '3'  
	message['X-MSMail-Priority'] =  'Normal'  
	message['X-Mailer'] =  'Microsoft Outlook Express 6.00.2900.2180'  
	message['X-MimeOLE'] =  'Produced By Microsoft MimeOLE V6.00.2900.2180'  
	  
	with open(config['file'], 'rb') as f:  
	    file = MIMEApplication(f.read())  
	file.add_header('Content-Disposition', 'attachment', filename=os.path.basename(config['file']))  
	message.attach(file)  
	  
	print 'OK'  
	print 'Logging...',  
	  
	smtp = SMTP(config['server'], config['port'])  
	smtp.ehlo()  
	smtp.starttls()  
	smtp.ehlo()  
	smtp.login(config['username'], config['password'])  
	  
	print 'OK'  
	print 'Sending...',  
	  
	smtp.sendmail(config['from'], [config['from'], config['to']], message.as_string())  
	  
	print 'OK'  
	  
	smtp.close()  
	  
	#time.sleep(1)  
	
send_file_to_gmail(r'D:\hello.txt')
